package cn.hahah.module.blog.app.service.Impl;

import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import cn.hahah.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.hahah.module.blog.app.SensitiveWordUtil;
import cn.hahah.module.blog.app.service.BlogUserService;
import cn.hahah.module.blog.controller.admin.article.vo.ArticlePageReqVO;
import cn.hahah.module.blog.controller.admin.article.vo.ArticleRespVO;
import cn.hahah.module.blog.controller.admin.article.vo.ArticleSaveReqVO;
import cn.hahah.module.blog.controller.admin.articletag.vo.ArticleTagSaveReqVO;
import cn.hahah.module.blog.controller.admin.usercollect.vo.UserCollectSaveReqVO;
import cn.hahah.module.blog.controller.admin.usercomment.vo.UserCommentSaveReqVO;
import cn.hahah.module.blog.controller.admin.userdialogue.vo.UserDialogueSaveReqVO;
import cn.hahah.module.blog.controller.admin.userdialoguegroup.vo.UserDialogueGroupSaveReqVO;
import cn.hahah.module.blog.controller.admin.userdialoguepersonal.vo.UserDialoguePersonalSaveReqVO;
import cn.hahah.module.blog.controller.admin.userfocus.vo.UserFocusSaveReqVO;
import cn.hahah.module.blog.controller.admin.userfriend.vo.UserFriendSaveReqVO;
import cn.hahah.module.blog.controller.admin.userhistorybrowse.vo.UserHistoryBrowsePageReqVO;
import cn.hahah.module.blog.controller.admin.userlike.vo.UserLikeSaveReqVO;
import cn.hahah.module.blog.controller.admin.userreply.vo.UserReplySaveReqVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersPageReqVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersRespVO;
import cn.hahah.module.blog.controller.admin.users.vo.UsersSaveReqVO;
import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.articletag.ArticleTagDO;
import cn.hahah.module.blog.dal.dataobject.articletype.ArticleTypeDO;
import cn.hahah.module.blog.dal.dataobject.tag.TagDO;
import cn.hahah.module.blog.dal.dataobject.usercollect.UserCollectDO;
import cn.hahah.module.blog.dal.dataobject.userdialogue.UserDialogueDO;
import cn.hahah.module.blog.dal.dataobject.userdialoguepersonal.UserDialoguePersonalDO;
import cn.hahah.module.blog.dal.dataobject.userfocus.UserFocusDO;
import cn.hahah.module.blog.dal.dataobject.userfriend.UserFriendDO;
import cn.hahah.module.blog.dal.dataobject.userhistorybrowse.UserHistoryBrowseDO;
import cn.hahah.module.blog.dal.dataobject.userlike.UserLikeDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.dal.mysql.article.ArticleMapper;
import cn.hahah.module.blog.dal.mysql.articletag.ArticleTagMapper;
import cn.hahah.module.blog.dal.mysql.usercollect.UserCollectMapper;
import cn.hahah.module.blog.dal.mysql.userdialogue.UserDialogueMapper;
import cn.hahah.module.blog.dal.mysql.userdialoguepersonal.UserDialoguePersonalMapper;
import cn.hahah.module.blog.dal.mysql.userfocus.UserFocusMapper;
import cn.hahah.module.blog.dal.mysql.userfriend.UserFriendMapper;
import cn.hahah.module.blog.dal.mysql.userlike.UserLikeMapper;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.articletag.ArticleTagService;
import cn.hahah.module.blog.service.articletype.ArticleTypeService;
import cn.hahah.module.blog.service.tag.TagService;
import cn.hahah.module.blog.service.usercollect.UserCollectService;
import cn.hahah.module.blog.service.usercomment.UserCommentService;
import cn.hahah.module.blog.service.userdialogue.UserDialogueService;
import cn.hahah.module.blog.service.userdialoguegroup.UserDialogueGroupService;
import cn.hahah.module.blog.service.userdialoguepersonal.UserDialoguePersonalService;
import cn.hahah.module.blog.service.userfocus.UserFocusService;
import cn.hahah.module.blog.service.userfriend.UserFriendService;
import cn.hahah.module.blog.service.userhistorybrowse.UserHistoryBrowseService;
import cn.hahah.module.blog.service.userlike.UserLikeService;
import cn.hahah.module.blog.service.userreply.UserReplyService;
import cn.hahah.module.blog.service.users.UsersService;
import cn.hutool.dfa.SensitiveUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;


@Service
@Transactional
public class BlogUserServiceImpl implements BlogUserService {
    @Resource
    private UsersService usersService;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleTagService articleTagService;
    @Resource
    private ArticleTagMapper articleTagMapper;
    @Resource
    private TagService tagService;
    @Resource
    private UserFocusService userFocusService;
    @Resource
    private UserFocusMapper userFocusMapper;
    @Resource
    private UserCollectService userCollectService;
    @Resource
    private UserCollectMapper userCollectMapper;
    @Resource
    private UserFriendService userFriendService;
    @Resource
    private UserFriendMapper userFriendMapper;
    @Resource
    private ArticleTypeService articleTypeService;
    @Resource
    private UserLikeService userLikeService;
    @Resource
    private UserLikeMapper userLikeMapper;
    @Resource
    private UserCommentService userCommentService;
    @Resource
    private UserReplyService userReplyService;
    @Resource
    private UserHistoryBrowseService userHistoryBrowseService;
    @Resource
    private UserDialoguePersonalService userDialoguePersonalService;
    @Resource
    private UserDialoguePersonalMapper userDialoguePersonalMapper;
    @Resource
    private UserDialogueMapper userDialogueMapper;
    @Resource
    private UserDialogueGroupService userDialogueGroupService;
    @Resource
    private UserDialogueService userDialogueService;
    @Resource
    private SensitiveWordUtil sensitiveWordUtil;

    @Override
    public CommonResult<UsersRespVO> userSet(Long userId, UsersPageReqVO usersPageReqVO){
        UsersDO usersDO = usersService.getUsers(userId);
        if(usersDO == null){
            return CommonResult.error(400, "不存在用户");
        }

        UsersSaveReqVO usersSaveReqVO = BeanUtils.toBean(usersPageReqVO, UsersSaveReqVO.class);
        usersSaveReqVO.setId(userId);
        usersSaveReqVO.setPassword(usersDO.getPassword());
        usersService.updateUsers(usersSaveReqVO);

        usersDO = usersService.getUsers(userId).setPassword(null);
        return CommonResult.success(BeanUtils.toBean(usersDO, UsersRespVO.class));
    }

    @Override
    public CommonResult<String> userCreateArt(ArticleSaveReqVO articleSaveReqVO) {
        boolean exist = articleMapper.exists(new LambdaQueryWrapperX<ArticleDO>()
                .eqIfPresent(ArticleDO::getTitle, articleSaveReqVO.getTitle()));
        if(exist){
            return CommonResult.error(400,"已存在同样的文章标题");
        }

        articleService.createArticle(articleSaveReqVO);
        return CommonResult.success("文章发布成功");
    }

    @Override
    public CommonResult<String> userUpdateArt(ArticleSaveReqVO articleSaveReqVO){
        ArticleDO articleDO = articleService.getArticle(articleSaveReqVO.getId());
        if(articleDO == null){
            return CommonResult.error(400, "文章不存在");
        }

//        List<ArticleDO> articleDOList = articleMapper.selectList(
//                new LambdaQueryWrapperX<ArticleDO>()
//                .eqIfPresent(ArticleDO::getTitle, articleSaveReqVO.getTitle()));
//        ArticleDO articleDO1 = articleDOList.get(0);
//        if(articleDOList.size() > 1){
//            return CommonResult.error(400, "已存在同样的文章标题");
//        }
//        if(!Objects.equals(articleDO1.getId(), articleSaveReqVO.getId())){
//            return CommonResult.error(400, "已存在同样的文章标题");
//        }

        articleService.updateArticle(articleSaveReqVO);
        return CommonResult.success("文章修改成功");
    }

    @Override
    public CommonResult<PageResult<ArticleRespVO>> getUserArtPage(ArticlePageReqVO articlePageReqVO) {

        PageResult<ArticleDO> pageResult = articleMapper.selectPage(articlePageReqVO);
        for (ArticleDO articleDO : pageResult.getList()) {
            articleDO.setContent("空");
        }

        return CommonResult.success(BeanUtils.toBean(pageResult, ArticleRespVO.class));
    }

    @Override
    public CommonResult<String> delUserArtPage(Long userId, Long articleId){
        ArticleDO articleDO = articleService.getArticle(articleId);
        if(!Objects.equals(articleDO.getUserId(), userId)){
            return CommonResult.error(400, "不是你的文档");
        }
        articleService.deleteArticle(articleId);
        return CommonResult.success("删除成功");
    }

    @Override
    public CommonResult<List<ArticleRespVO>> getUserCollectArt(
            ArticlePageReqVO articlePageReqVO){
        List<UserCollectDO> userCollectDOList = userCollectMapper.selectList(new LambdaQueryWrapper<UserCollectDO>()
                .eq(UserCollectDO::getUserId, articlePageReqVO.getUserId()));

        List<ArticleRespVO> articleDOS = new ArrayList<>();
        for (UserCollectDO userCollectDO : userCollectDOList) {
            Long articleId = userCollectDO.getArticleId();
            ArticleDO articleDO = articleService.getArticle(articleId);
            articleDO.setContent("空");
            articleDOS.add(BeanUtils.toBean(articleDO, ArticleRespVO.class));
        }

//        int total = articleDOS.size();
//        int pageSize = articlePageReqVO.getPageSize();
//        int pageNum = articlePageReqVO.getPageNo();
//        int sub = total - pageSize;
//        int totalPage = sub > 0 ? total/pageSize : 1 ;
//
//        for (int i = 0; i > (pageSize-1)*pageNum-1 && i < (pageSize*pageNum)+pageSize; i++) {
//
//        }
        return CommonResult.success(articleDOS);
    }

    @Override
    public CommonResult<String> unCollectArt(Long userId, Long articleId){
        List<UserCollectDO> userCollectDOList = userCollectMapper.selectList(new LambdaQueryWrapper<UserCollectDO>()
                .eq(UserCollectDO::getUserId, userId));


        for (UserCollectDO userCollectDO : userCollectDOList) {
            if(Objects.equals(userCollectDO.getArticleId(), articleId)){
                userCollectService.deleteUserCollect(userCollectDO.getId());
            }
        }

        return CommonResult.success("取消收藏成功");
    }

    @Override
    public CommonResult<String> collectArt(Long userId, Long articleId) {

        if(articleService.getArticle(articleId) == null){
            return CommonResult.error(400, "文章不存在");
        }

        UserCollectSaveReqVO userCollectSaveReqVO = new UserCollectSaveReqVO();
        userCollectSaveReqVO.setUserId(userId);
        userCollectSaveReqVO.setArticleId(articleId);

        List<UserCollectDO> userCollectDOS =  userCollectMapper.selectList(new LambdaQueryWrapperX<UserCollectDO>()
                .eqIfPresent(UserCollectDO::getUserId, userCollectSaveReqVO.getUserId())
                .eqIfPresent(UserCollectDO::getArticleId, userCollectSaveReqVO.getArticleId()));
        if(!userCollectDOS.isEmpty()){
            return CommonResult.error(400, "文章已收藏");
        }

        userCollectService.createUserCollect(userCollectSaveReqVO);
        return CommonResult.success("文章收藏成功");
    }

    @Override
    public CommonResult<String> likeArt(Long userId, Long articleId) {

        if(articleService.getArticle(articleId) == null){
            return CommonResult.error(400, "文章不存在");
        }

        List<UserLikeDO> userLikeDOS = userLikeMapper.selectList(new LambdaQueryWrapper<UserLikeDO>()
                .eq(UserLikeDO::getUserId, userId)
                .eq(UserLikeDO::getArticleId, articleId));
        for (UserLikeDO userLikeDO : userLikeDOS) {
            userLikeService.deleteUserLike(userLikeDO.getId());
        }

        UserLikeSaveReqVO vo = new UserLikeSaveReqVO();
        vo.setUserId(userId);
        vo.setArticleId(articleId);
        vo.setIsLike(true);
        userLikeService.createUserLike(vo);
        return CommonResult.success("点赞成功");
    }

    @Override
    public CommonResult<String> setArticleType(Long userId, Long articleId, Long typeId) {
        ArticleDO articleDO = articleService.getArticle(articleId);
        if(articleDO == null){
            CommonResult.error(400, "文章不存在");
        }
        if(!Objects.equals(articleDO.getUserId(), userId)){
            return CommonResult.error(400, "文章不属于你");
        }
        ArticleTypeDO articleTypeDO = articleTypeService.getArticleType(typeId);
        if(articleTypeDO == null){
            return CommonResult.error(400, "分类不存在");
        }


        ArticleSaveReqVO articleTagSaveReqVO = BeanUtils.toBean(articleDO, ArticleSaveReqVO.class);
        articleTagSaveReqVO.setTypeId(typeId);
        articleService.updateArticle(articleTagSaveReqVO);
        return CommonResult.success("设置成功");
    }

    @Override
    public CommonResult<String> setArticleTag(Long userId, Long articleId, List<Long>  selectTagId){
        ArticleDO articleDO = articleService.getArticle(articleId);
        if(articleDO == null){
            CommonResult.error(400, "文章不存在");
        }
        if(!Objects.equals(articleDO.getUserId(), userId)){
            return CommonResult.error(400, "文章不属于你");
        }

        articleTagMapper.delete(new LambdaQueryWrapper<ArticleTagDO>()
                .eq(ArticleTagDO::getArticleId, articleId));

        
        for (Long tagId : selectTagId) {
            TagDO tagDO = tagService.getTag(tagId);
            if(tagDO == null){
                return CommonResult.error(400, "标签不存在");
            }

            ArticleTagSaveReqVO articleTagSaveReqVO = new ArticleTagSaveReqVO();
            articleTagSaveReqVO.setTagId(tagId);
            articleTagSaveReqVO.setArticleId(articleId);
            articleTagService.createArticleTag(articleTagSaveReqVO);
        }
        return CommonResult.success("设置成功");
    }

    @Override
    public CommonResult<List<UsersDO>> getUserFocusById(Long userId) {
        List<UserFocusDO> userFocusDOS = userFocusMapper.selectList(new LambdaQueryWrapper<UserFocusDO>()
                .eq(UserFocusDO::getUserId, userId));

        if(userFocusDOS.isEmpty()){
            CommonResult.success(null);
        }

        List<UsersDO> usersDOList = new ArrayList<>();
        for (UserFocusDO userFocusDO : userFocusDOS) {
            usersDOList.add(usersService.getUsers(userFocusDO.getFocusId()));
        }
        return CommonResult.success(usersDOList);
    }

    @Override
    public CommonResult<String> userFocus(UserFocusSaveReqVO userFocusSaveReqVO) {
        List<UserFocusDO> userFocusDOS =  userFocusMapper.selectList(new LambdaQueryWrapperX<UserFocusDO>()
                .eqIfPresent(UserFocusDO::getUserId, userFocusSaveReqVO.getUserId())
                .eqIfPresent(UserFocusDO::getFocusId, userFocusSaveReqVO.getFocusId()));
        if(!userFocusDOS.isEmpty()){
            return CommonResult.success("已关注用户" );
        }

        userFocusService.createUserFocus(userFocusSaveReqVO);
        return CommonResult.success("用户关注成功");
    }

    @Override
    public CommonResult<String> unFocusUserById(Long userId, Long focusId) {
        List<UserFocusDO> userFocusDOS =  userFocusMapper.selectList(new LambdaQueryWrapperX<UserFocusDO>()
                .eqIfPresent(UserFocusDO::getUserId, userId)
                .eqIfPresent(UserFocusDO::getFocusId, focusId));

        if(userFocusDOS.isEmpty()){
            CommonResult.success("你还没有关注该用户");
        }

        for (UserFocusDO userFocusDO : userFocusDOS) {
            userFocusService.deleteUserFocus(userFocusDO.getId());
        }
        return CommonResult.success("用户取消关注成功");
    }

    @Override
    public CommonResult<String> commentArt(UserCommentSaveReqVO userCommentSaveReqVO) {
        boolean exist = sensitiveWordUtil.containsSensitiveWord(userCommentSaveReqVO.getContent());
        if(exist){
            return CommonResult.error(400, "你的评论包含敏感词，评论失败");
        }

        userCommentService.createUserComment(userCommentSaveReqVO);
        return CommonResult.success("评论成功");
    }

    @Override
    public CommonResult<String> replyComment(UserReplySaveReqVO userReplySaveReqVO) {

        userReplyService.createUserReply(userReplySaveReqVO);
        return CommonResult.success("回复成功");
    }

    @Override
    public CommonResult<String> addFriend(Long userId, Long friendId) {
        List<UserFriendDO> userFriendDOS1 =  userFriendMapper.selectList(new LambdaQueryWrapperX<UserFriendDO>()
                .eqIfPresent(UserFriendDO::getUserId, userId)
                .eqIfPresent(UserFriendDO::getFriendId, friendId));
        List<UserFriendDO> userFriendDOS2 =  userFriendMapper.selectList(new LambdaQueryWrapperX<UserFriendDO>()
                .eqIfPresent(UserFriendDO::getUserId, friendId)
                .eqIfPresent(UserFriendDO::getFriendId, userId));


        if(!userFriendDOS1.isEmpty() || !userFriendDOS2.isEmpty()){
            return CommonResult.error(400, "已存在或已申请好友");
        }

        UserFriendSaveReqVO userFriendSaveReqVO = new UserFriendSaveReqVO();
        userFriendSaveReqVO.setUserId(userId);
        userFriendSaveReqVO.setFriendId(friendId);
        userFriendSaveReqVO.setStatus(false);
        userFriendService.createUserFriend(userFriendSaveReqVO);
        return CommonResult.success("好友请求发送成功");
    }

    @Override
    public CommonResult<List<UsersDO>> getFriendByUser(Long userId) {
        List<UserFriendDO> userFriendDOList1 = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getUserId, userId)
                .eq(UserFriendDO::getStatus, true));
        List<UserFriendDO> userFriendDOList2 = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getFriendId, userId)
                .eq(UserFriendDO::getStatus, true));


        List<Long> idList = new ArrayList<>();
        for (UserFriendDO userFriendDO : userFriendDOList1) {
            idList.add(userFriendDO.getFriendId());
        }
        for (UserFriendDO userFriendDO : userFriendDOList2) {
            idList.add(userFriendDO.getUserId());
        }
        List<Long> uniqueList = new ArrayList<>(new HashSet<>(idList));



        List<UsersDO> usersDOList = new ArrayList<>();
        for (Long id : uniqueList) {
            usersDOList.add(usersService.getUsers(id));
        }
        return CommonResult.success(usersDOList);
    }

    @Override
    public CommonResult<List<UsersDO>> getFriendReq(Long userId) {
        List<UserFriendDO> userFriendDOList = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getFriendId, userId)
                .eq(UserFriendDO::getStatus, false));


        List<UsersDO> usersDOList = new ArrayList<>();
        for (UserFriendDO userFriendDO : userFriendDOList) {
            usersDOList.add(usersService.getUsers(userFriendDO.getUserId()));
        }
        return CommonResult.success(usersDOList);
    }

    @Override
    public CommonResult<String> agreeFriendRea(Long userId, Long friendId){
        List<UserFriendDO> userFriendDOList = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getFriendId, userId)
                .eq(UserFriendDO::getUserId, friendId)
                .eq(UserFriendDO::getStatus, false));

        UserFriendDO userFriendDO = userFriendDOList.get(0);
        UserFriendSaveReqVO userFriendVo = new UserFriendSaveReqVO();
        userFriendVo.setId(userFriendDO.getId());
        userFriendVo.setUserId(userFriendDO.getUserId());
        userFriendVo.setFriendId(userFriendDO.getFriendId());
        userFriendVo.setStatus(true);

        userFriendService.updateUserFriend(userFriendVo);
        return CommonResult.success("请求通过");
    }

    @Override
    public CommonResult<String> rejuctFriendRea(Long userId, Long friendId){
        List<UserFriendDO> userFriendDOList = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getFriendId, userId)
                .eq(UserFriendDO::getUserId, friendId)
                .eq(UserFriendDO::getStatus, false));

        UserFriendDO userFriendDO = userFriendDOList.get(0);
        userFriendService.deleteUserFriend(userFriendDO.getId());
        return CommonResult.success("已拒绝");
    }

    @Override
    public CommonResult<String> delFriend(Long userId, Long friendId) {
        List<UserFriendDO> userFriendDOList1 = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getFriendId, userId)
                .eq(UserFriendDO::getUserId, friendId)
                .eq(UserFriendDO::getStatus, true));
        List<UserFriendDO> userFriendDOList2 = userFriendMapper.selectList(new LambdaQueryWrapper<UserFriendDO>()
                .eq(UserFriendDO::getFriendId, friendId)
                .eq(UserFriendDO::getUserId, userId)
                .eq(UserFriendDO::getStatus, true));

        UserFriendDO userFriendDO;
        if(!userFriendDOList1.isEmpty()){
            userFriendDO = userFriendDOList1.get(0);
            userFriendService.deleteUserFriend(userFriendDO.getId());
            return CommonResult.success("删除成功");
        }
        if(!userFriendDOList2.isEmpty()){
            userFriendDO = userFriendDOList2.get(0);
            userFriendService.deleteUserFriend(userFriendDO.getId());
            return CommonResult.success("删除成功");
        }
        return CommonResult.error(400, "找不到好友");
    }

    @Override
    public CommonResult<PageResult<ArticleDO>> browseHistory(
            Integer pageNo,
            Integer pageSize,
            Long userId){

        UserHistoryBrowsePageReqVO vo = new UserHistoryBrowsePageReqVO();
        vo.setUserId(userId);
        vo.setPageNo(pageNo);
        vo.setPageSize(pageSize);
        PageResult<UserHistoryBrowseDO> pageResult = userHistoryBrowseService.getUserHistoryBrowsePage(vo);


        PageResult<ArticleDO> pageResult2 = new PageResult<>();
        List<ArticleDO> articleDOS = new ArrayList<>();
        pageResult2.setList(articleDOS);
        for (UserHistoryBrowseDO userHistoryBrowseDO : pageResult.getList()) {
            ArticleDO articleDO = articleService.getArticle(userHistoryBrowseDO.getArticleId());
            articleDO.setContent("空");
            articleDO.setCreateTime(userHistoryBrowseDO.getCreateTime());
            pageResult2.getList().add(articleDO);
        }
        pageResult2.setTotal(pageResult.getTotal());


        return CommonResult.success(pageResult2);
    }

    @Override
    public CommonResult<List<UserDialogueDO>> userDialogue(Long userId, Long targetUserId) {
        if(Objects.equals(userId, targetUserId)){
            return CommonResult.error(400, "你不能跟自己对话");
        }

        //验证是否有"聊天记录"存在
        Long groupId = dialogIsExist(userId, targetUserId);
        if(groupId!=null){
            //存在，就返回 聊天记录的内容
            List<UserDialogueDO> userDialogList = userDialogueMapper.selectList(new LambdaQueryWrapper<UserDialogueDO>()
                    .eq(UserDialogueDO::getGroupId, groupId));
            return CommonResult.success(userDialogList);

        }else{
            //不存在，就创建聊天记录，返回空值
            UserDialogueGroupSaveReqVO vo = new UserDialogueGroupSaveReqVO();
            Long createdId = userDialogueGroupService.createUserDialogueGroup(vo);
            UserDialoguePersonalSaveReqVO userVo = new UserDialoguePersonalSaveReqVO();
            userVo.setGroupId(createdId);
            userVo.setUserId(userId);
            UserDialoguePersonalSaveReqVO targetVo = new UserDialoguePersonalSaveReqVO();
            targetVo.setGroupId(createdId);
            targetVo.setUserId(targetUserId);
            userDialoguePersonalService.createUserDialoguePersonal(userVo);
            userDialoguePersonalService.createUserDialoguePersonal(targetVo);

            return CommonResult.success(null);
        }
    }

    public Long dialogIsExist(Long userId, Long targetUserId){
        List<UserDialoguePersonalDO> userDList = userDialoguePersonalMapper.selectList(
                new LambdaQueryWrapper<UserDialoguePersonalDO>()
                .eq(UserDialoguePersonalDO::getUserId, userId));

        List<UserDialoguePersonalDO> targetDList = userDialoguePersonalMapper.selectList(
                new LambdaQueryWrapper<UserDialoguePersonalDO>()
                .eq(UserDialoguePersonalDO::getUserId, targetUserId));

        Long groupId = null;
        System.out.println(userDList);
        System.out.println(targetDList);
        for (UserDialoguePersonalDO userDialoguePersonalDO : userDList) {
            for (UserDialoguePersonalDO dialoguePersonalDO : targetDList) {
                if(Objects.equals(dialoguePersonalDO.getGroupId(), userDialoguePersonalDO.getGroupId())){
                    groupId = dialoguePersonalDO.getGroupId();
                }
            }
        }

        return groupId;
    }

    @Override
    public CommonResult<List<UsersDO>> getUserDialogue(Long userId) {
        List<UserDialoguePersonalDO> userDList = userDialoguePersonalMapper.selectList(
                new LambdaQueryWrapper<UserDialoguePersonalDO>()
                .eq(UserDialoguePersonalDO::getUserId, userId));

        List<UsersDO> usersDOList = new ArrayList<>();
        for (UserDialoguePersonalDO userDialoguePersonalDO : userDList) {

            List<UserDialoguePersonalDO> groupList = userDialoguePersonalMapper.selectList(
                new LambdaQueryWrapper<UserDialoguePersonalDO>()
                .eq(UserDialoguePersonalDO::getGroupId, userDialoguePersonalDO.getGroupId()));

            for (UserDialoguePersonalDO dialoguePersonalDO : groupList) {
                if(!Objects.equals(dialoguePersonalDO.getUserId(), userId)){
                    UsersDO usersDO = usersService.getUsers(dialoguePersonalDO.getUserId());
                    usersDO.setPassword("空");
                    usersDO.setId(dialoguePersonalDO.getGroupId());
                    usersDOList.add(usersDO);
                }
            }

        }

        return CommonResult.success(usersDOList);

    }

    @Override
    public CommonResult<List<UserDialogueDO>> getDialogues(Long groupId) {
        List<UserDialogueDO> userDialogueDOList = userDialogueMapper.selectList(new LambdaQueryWrapper<UserDialogueDO>()
                .eq(UserDialogueDO::getGroupId, groupId));

        return CommonResult.success(userDialogueDOList);
    }

    @Override
    public CommonResult<String> sendMessage(Long userId, Long groupId, String content) {
        UserDialogueSaveReqVO vo = new UserDialogueSaveReqVO();
        vo.setUserId(userId);
        vo.setGroupId(groupId);
        vo.setContent(content);
        userDialogueService.createUserDialogue(vo);

        return CommonResult.success("消息发送成功");
    }
}
